package cn.xnatural.demo.ctrl;

import cn.xnatural.app.Bean;
import cn.xnatural.app.Lazier;
import cn.xnatural.app.ServerTpl;
import cn.xnatural.app.Utils;
import cn.xnatural.demo.service.FileUploader;
import cn.xnatural.xchain.Route;
import cn.xnatural.xnet.HttpContext;
import cn.xnatural.xnet.HttpServer;

import java.io.File;

import static cn.xnatural.app.Utils.baseDir;

@Bean
@Route(path = "/", protocol = "HTTP")
public class MainCtrl extends ServerTpl {

    final Lazier<String> _staticDir = new Lazier<>(() -> bean(HttpServer.class).getAttr("http.staticDir", String.class, "src/test/main/resources/static/"));
    final Lazier<Boolean> _isPro = new Lazier<>(() -> "pro".equals(app().getAttr("profile", String.class, null)));


    @Route(path = {"index.html", "/"})
    File index(HttpContext ctx) {
        ctx.response.cacheControl(10);
        return baseDir(_staticDir.get() + "/index.html");
    }

    @Route(path = "test.html")
    File testHtml(HttpContext ctx) {
        //ctx.response.cacheControl(3);
        return baseDir(_staticDir.get() + "/test.html");
    }

    @Route(path = "favicon.ico")
    File favicon(HttpContext hCtx) {
        hCtx.response.cacheControl(3000);
        hCtx.response.contentType("image/x-icon");
        return Utils.baseDir(_staticDir.get() + "/favicon.ico");
    }


    // 文件
    @Route(path = "file/{fName}")
    File file(String fName) { return bean(FileUploader.class).findFile(fName); }


    // ==========================js =====================

    @Route(path = "js/{fName}")
    File js(String fName, HttpContext ctx, HttpServer server) {
        if (_isPro.get()) {
            ctx.response.cacheControl(1800);
        }
        return baseDir("src/test/main/resources/static/js/" + fName);
    }
    @Route(path = "js/lib/{fName}")
    File js_lib(String fName, HttpContext ctx) {
        ctx.response.cacheControl(1800);
        return baseDir("src/test/main/resources/static/js/lib/" + fName);
    }

    @Route(path = "components/{fName}")
    File components(String fName, HttpContext ctx, HttpServer server) {
        if (_isPro.get()) {
            ctx.response.cacheControl(1800);
        }
        return baseDir("src/test/main/resources/static/components/" + fName);
    }

    @Route(path = "views/{fName}")
    File views(String fName, HttpContext hCtx) {
        if ("pro".equals(app().getProfile())) hCtx.response.cacheControl(1800);
        return Utils.baseDir("static/views/" + fName);
    }


    // =======================css ========================

    @Route(path = "css/{fName}")
    File css(String fName, HttpContext ctx, HttpServer server) {
        if (_isPro.get()) {
            ctx.response.cacheControl(1800);
        }
        return baseDir("src/test/main/resources/static/css/" + fName);
    }

    @Route(path = "css/fonts/{fName}")
    File css_fonts(String fName, HttpContext ctx) {
        ctx.response.cacheControl(1800);
        return baseDir("src/test/main/resources/static/css/fonts/" + fName);
    }

    @Route(path = "css/lib/{fName}")
    File css_lib(String fName, HttpContext ctx) {
        ctx.response.cacheControl(1800);
        return baseDir("src/test/main/resources/static/css/lib/" + fName);
    }



    // ================= 图片 ======================
    @Route(path = "img/{fName}")
    File img(String fName, HttpContext hCtx) {
        hCtx.response.cacheControl(172800);
        return Utils.baseDir("static/img/" + fName);
    }
}
